package com.parseword2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Word{
	private Map<Character, Word> wordMap = new HashMap<Character, Word>();
	private static final long serialVersionUID = 1L;
	private char node;	// 文字节点
	private boolean end;
	
	
	public Word(char node) {
		this.node = node;
	}
	public Word() {
	}
	/**
	 * 说明：设置词汇		<br>
	 * 时间：2017年9月7日 下午2:46:33
	 *
	 * @param text
	 */
	public void set(String text){
		Word rootNode = this;
		int length = text.length();
		for (int i = 0; i < length; i++) {
			char c = text.charAt(i);
			
			Word wordNode = rootNode.get(c);
			if(wordNode == null){
				wordNode = new Word(c);
				rootNode.put(c, wordNode);
			}
			rootNode = wordNode;
		}
		rootNode.end = true;
	}
	protected void put(Character key,Word value) {
		wordMap.put(key, value);
	}
	public Word get(Character key){
		return wordMap.get(key);
	}
	public ParseWordResult get(String text,ParseWord parseWord){
		ParseParams parseParams = new ParseParams();
		parseParams.setRootNode(this);
		parseParams.setText(text);
		return parseWord.parse(parseParams);
	}
	
	public List<ParseWordResult> getAll(String text,ParseWord parseWord){
		List<ParseWordResult> parseWordResults = new ArrayList<ParseWordResult>();
		ParseWordResult parseWordResult = get(text, parseWord);
		parseWordResults.add(parseWordResult);
		while(parseWordResult.getRearText()!=null&&!parseWordResult.getRearText().isEmpty()){
			parseWordResult = get(parseWordResult.getRearText(), parseWord);
			if(parseWordResult.getWordText()!=null){
				parseWordResults.add(parseWordResult);
			}
		}
		return parseWordResults;
	}
	
	public boolean isEnd() {
		return end;
	}
}
